<template>
  <div class="app-container">
    <div class="search-container">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item prop="keywords" label="名称">
          <el-input
            v-model="queryParams.keywords"
            placeholder="输入搜索关键词"
            clearable
          />
        </el-form-item>

        <el-form-item>
          <el-button type="primary" @click="handleQuery"
            ><i-ep-search />搜索</el-button
          >

          <el-button @click="resetQuery"><i-ep-refresh />重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-card shadow="never" class="table-container">
      <template #header> </template>
      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="tableData"
        highlight-current-row
        :stripe="true"
        size="large"
        max-height="600px"
      >
        <el-table-column align="center" label="序号" type="index" width="100" />

        <el-table-column
          align="center"
          width="120"
          label="资源名称"
          prop="resourceName"
        />

        <el-table-column
          align="center"
          width="200"
          label="开始时间"
          prop="startTime"
        />
        <el-table-column
          align="center"
          width="200"
          label="结束时间"
          prop="endTime"
        />

        <el-table-column
          align="center"
          width="200"
          label="驳回原因"
          prop="reason"
        />
        <el-table-column
          align="center"
          label="实际结束时间"
          width="200"
          prop="realEndTime"
        />

        <el-table-column
          fixed="right"
          align="center"
          label="状态"
          width="120"
          prop="status"
        >
          <template #default="scope">
            <el-tag v-if="scope.row.status === 0" type="warning">待审核</el-tag>
            <el-tag v-if="scope.row.status === 1" type="danger">驳回</el-tag>
            <el-tag v-if="scope.row.status === 2" type="success">通过</el-tag>
            <el-tag v-if="scope.row.status === 3">完成</el-tag>
            <el-tag v-if="scope.row.status === 4" type="info">取消</el-tag>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { getMyResourceRecordAPI } from "@/api/resourceRecord/index";
import { cancelAPI } from "@/api/resource/index";

import { IPage, IResourceRecordVo } from "@/api/resourceRecord/types";

import { ref, reactive } from "vue";

const total = ref(0);
const loading = ref(false);
const tableData = ref<IResourceRecordVo[]>([]);
const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);
// 表单数据
const formData = reactive<IResourceRecordVo>({});
// 表单规则
const rules = reactive({
  title: [{ required: true, message: "请输入", trigger: "blur" }],
});

const dialog = reactive<DialogOption>({
  visible: false,
});
const queryParams = reactive<IPage>({
  pageNum: 1,
  pageSize: 10,
});

onMounted(() => {
  handleQuery();
});

// 获取预约记录列表
function handleQuery() {
  loading.value = true;
  getMyResourceRecordAPI()
    .then(({ data }) => {
      tableData.value = data;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置查询 */

function resetQuery() {
  queryParams.pageNum = 1;
  handleQuery();
}

/** 关闭表单弹窗 */
function closeDialog() {
  dialog.visible = false;
  resetForm();
}

/** 重置表单 */
function resetForm() {
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}
/** 取消成功 */
function cancel(id: number) {
  loading.value = true;
  cancelAPI(id)
    .then(() => {
      ElMessage.success("取消成功");
      resetQuery();
    })
    .finally(() => (loading.value = false));
  resetForm();
}
</script>

<style lang="scss" scoped></style>
